//
//  GoodsInfoBottomView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/1.
//

#import "GoodsInfoBottomView.h"

@interface GoodsInfoBottomView ()

@property (nonatomic, strong) UILabel *priceLabel;
/// 删除
@property (nonatomic, strong) UIButton *deleteBtn;
/// 上架下架
@property (nonatomic, strong) UIButton *downBtn;
/// 置顶
@property (nonatomic, strong) UIButton *topBtn;
/// 编辑
@property (nonatomic, strong) UIButton *editBtn;
/// 售罄
@property (nonatomic, strong) UIButton *shouqingBtn;
/// 更新
@property (nonatomic, strong) UIButton *updateBtn;
/// 一键转图
@property (nonatomic, strong) UIButton *forwardBtn;
/// 编辑转图
@property (nonatomic, strong) UIButton *editTransBtn;

@end

@implementation GoodsInfoBottomView

- (void)initViews {
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.deleteBtn];
    [self addSubview:self.downBtn];
    [self addSubview:self.editBtn];
    [self addSubview:self.topBtn];
    [self addSubview:self.shouqingBtn];
    
    [self addSubview:self.priceLabel];
    [self addSubview:self.updateBtn];
    [self addSubview:self.editTransBtn];
    [self addSubview:self.forwardBtn];
    
    CGFloat w = (kWidthForScreen - 72)/5;
    [self.shouqingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(w, 24));
        make.top.mas_equalTo(15);
    }];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shouqingBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.editBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.downBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.downBtn.mas_trailing).offset(10);
        make.size.bottom.equalTo(self.shouqingBtn);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.top.mas_equalTo(15);
    }];
    [self.updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.size.mas_equalTo(CGSizeMake(w, 24));
        make.centerY.equalTo(self.priceLabel);
    }];
    [self.forwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.updateBtn.mas_leading).offset(-10);
        make.size.mas_equalTo(CGSizeMake(w, 24));
        make.centerY.equalTo(self.priceLabel);
    }];
    [self.editTransBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.forwardBtn.mas_leading).offset(-10);
        make.size.centerY.equalTo(self.forwardBtn);
    }];
}

- (void)setModel:(ShopModel *)model {
    _model = model;
    self.deleteBtn.hidden = self.downBtn.hidden = self.editBtn.hidden = self.topBtn.hidden = self.shouqingBtn.hidden = self.updateBtn.hidden =  !model.owner;
    self.priceLabel.hidden = model.owner;
    
    if (!model.owner && !model.trans) {
        self.forwardBtn.hidden = self.editTransBtn.hidden = NO;
    }else {
        self.forwardBtn.hidden = self.editTransBtn.hidden = YES;
    }
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@",model.sellingPrice];
    [self.downBtn setTitle:model.enabled == 1 ? @"下架" : @"上架" forState:UIControlStateNormal];
    [self.topBtn setTitle:model.top ? @"取消" : @"置顶" forState:UIControlStateNormal];
    [self.topBtn setTitleColor:model.top ? kRGB(0xFF9754) : [UIColor whiteColor] forState:UIControlStateNormal];
    self.topBtn.backgroundColor = model.top ? [UIColor whiteColor] : kRGB(0xFF9754);
    self.topBtn.layer.borderColor = kRGB(0xFF9754).CGColor;
    self.topBtn.layer.borderWidth = model.top ? 1 : 0;
    
    [self.updateBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(model.hasUpdate ? (kWidthForScreen - 72)/5 : 0);
    }];
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = kFontMediium(16);
        _priceLabel.textColor = kRGB(0xF15757);
        _priceLabel.hidden = YES;
        [_priceLabel sizeToFit];
    }
    return _priceLabel;
}
- (UIButton *)shouqingBtn {
    if (!_shouqingBtn) {
        _shouqingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shouqingBtn setTitle:@"售罄" forState:UIControlStateNormal];
        [_shouqingBtn setTitleColor:kBgColor forState:UIControlStateNormal];
        _shouqingBtn.titleLabel.font = kFontMediium(12);
        _shouqingBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.2);
        _shouqingBtn.layer.cornerRadius = 12;
        _shouqingBtn.layer.masksToBounds = YES;
        _shouqingBtn.hidden = YES;
        @weakify(self)
        [[_shouqingBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.shouqingBlock) {
                self.shouqingBlock(self.model);
            }
        }];
    }
    return _shouqingBtn;
}

- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _editBtn.backgroundColor = kRGB(0xFF6081);
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _editBtn.titleLabel.font = kFontMediium(12);
        _editBtn.layer.cornerRadius = 12;
        _editBtn.layer.masksToBounds = YES;
        _editBtn.hidden = YES;
        @weakify(self)
        [[_editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.editBlock) {
                self.editBlock(self.model);
            }
        }];
    }
    return _editBtn;
}

- (UIButton *)topBtn {
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.backgroundColor = kBgColor;
        [_topBtn setTitle:@"置顶" forState:UIControlStateNormal];
        [_topBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _topBtn.titleLabel.font = kFontMediium(12);
        _topBtn.layer.cornerRadius = 12;
        _topBtn.layer.masksToBounds = YES;
        _topBtn.hidden = YES;
        @weakify(self)
        [[_topBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topBlock) {
                self.topBlock(self.model);
            }
        }];
    }
    return _topBtn;
}

- (UIButton *)downBtn {
    if (!_downBtn) {
        _downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _downBtn.backgroundColor = kRGB(0xF2F2F2);
        [_downBtn setTitle:@"下架" forState:UIControlStateNormal];
        [_downBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _downBtn.titleLabel.font = kFontMediium(12);
        _downBtn.layer.cornerRadius = 12;
        _downBtn.layer.masksToBounds = YES;
        _downBtn.hidden = YES;
        @weakify(self)
        [[_downBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.topDownBlock) {
                self.topDownBlock(self.model);
            }
        }];
    }
    return _downBtn;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.backgroundColor = [UIColor whiteColor];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteBtn setTitleColor:kRGB(0xF15757) forState:UIControlStateNormal];
        _deleteBtn.titleLabel.font = kFontMediium(12);
        _deleteBtn.layer.cornerRadius = 12;
        _deleteBtn.layer.borderColor = kRGB(0xF15757).CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.layer.masksToBounds = YES;
        _deleteBtn.hidden = YES;
        @weakify(self)
        [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.deleteBlock) {
                self.deleteBlock(self.model);
            }
        }];
    }
    return _deleteBtn;
}

- (UIButton *)updateBtn {
    if (!_updateBtn) {
        _updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _updateBtn.backgroundColor = kRGB(0xFFD937);
        [_updateBtn setTitle:@"同步更新" forState:UIControlStateNormal];
        [_updateBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _updateBtn.titleLabel.font = kFontMediium(12);
        _updateBtn.layer.cornerRadius = 12;
        _updateBtn.layer.masksToBounds = YES;
        _updateBtn.hidden = YES;
        @weakify(self)
        [[_updateBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.updateBlock) {
                self.updateBlock(self.model);
            }
        }];
    }
    return _updateBtn;
}

- (UIButton *)forwardBtn {
    if (!_forwardBtn) {
        _forwardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forwardBtn setTitle:@"一键转图" forState:UIControlStateNormal];
        [_forwardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _forwardBtn.titleLabel.font = kFontMediium(12);
        _forwardBtn.backgroundColor = kBgColor;
        _forwardBtn.layer.cornerRadius = 12;
        _forwardBtn.layer.masksToBounds = YES;
        _forwardBtn.hidden = YES;
        @weakify(self)
        [[_forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.transBlock) {
                self.transBlock(self.model);
            }
        }];
    }
    return _forwardBtn;
}

- (UIButton *)editTransBtn {
    if (!_editTransBtn) {
        _editTransBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_editTransBtn setTitle:@"编辑转图" forState:UIControlStateNormal];
        [_editTransBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _editTransBtn.titleLabel.font = kFontMediium(12);
        _editTransBtn.backgroundColor = kRGB(0xFF6081);
        _editTransBtn.layer.cornerRadius = 12;
        _editTransBtn.layer.masksToBounds = YES;
        _editTransBtn.hidden = YES;
        @weakify(self)
        [[_editTransBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.editTransBlock) {
                self.editTransBlock(self.model);
            }
        }];
    }
    return _editTransBtn;
}

@end
